# 1.6 在图表中添加日期
import csv
from datetime import datetime
import matplotlib.pyplot as plt

# filename = 'data/sitka_weather_07-2018_simple.csv'
filename = 'data/sitka_weather_2018_simple.csv'

with open(filename) as f:
    reader = csv.reader(f)
    # 先读取掉表头
    header_row = next(reader)
    
    # 从文件中获取日期和最高温度
    # dates, highs = [], []
    dates, highs, lows = [], [], []
    for col in reader:
        current_date = datetime.strptime(col[2], '%Y-%m-%d')
        high = int(col[5])
        low = int(col[6])
        dates.append(current_date)
        highs.append(high)
        lows.append(low)

plt.style.use('seaborn')
fig, ax = plt.subplots()

# ax.plot(dates, highs, c='red')
# ax.plot(dates, lows, c='blue')

# 16.1.9 给图表区域着色
ax.plot(dates, highs, c='red', alpha=0.5)
ax.plot(dates, lows, c='blue', alpha=0.5)
ax.fill_between(dates, highs, lows, facecolor='green', alpha=0.1)

# ax.set_title("2018年7月每日最高温度", fontsize=24)
ax.set_title("2018年每日最高&最低温度", fontsize=24)
ax.set_xlabel('', fontsize=16)
# 自适应日期格式，防止重叠
fig.autofmt_xdate()
ax.set_ylabel('温度（F）', fontsize=16)
ax.tick_params(axis='both', which='major', labelsize=16)

plt.rcParams['font.sans-serif'] = ['SimHei']
plt.show()
